package com.Rest2Go;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MenuDataAdapter extends BaseAdapter
{

	ArrayList<DishDataItem>	mList;
	Context					mContext;
	private LayoutInflater	inflater;

	public MenuDataAdapter(ArrayList<DishDataItem> dishesList, Context context)
	{
		mList = dishesList;
		mContext = context;
		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount()
	{
		return mList.size();
	}

	@Override
	public Object getItem(int pos)
	{

		return mList.get(pos);
	}

	@Override
	public long getItemId(int pos)
	{
		return pos;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parentView)
	{
		LinearLayout view = (LinearLayout) convertView;
//		if (convertView == null)
//		{
			view = (LinearLayout) createView(pos);
		//}
		return view;
	}

	private View createView(int pos)
	{
		DishDataItem currObj = null;

		currObj = (DishDataItem) this.getItem(pos);

		DishDataItem objNew = (DishDataItem) this.getItem(pos);
		if (currObj != null)
		{
			if (currObj.isCat())
			{
				View menuCatView = (LinearLayout) inflater.inflate(R.layout.menu_category_layout, null);
				TextView catText = (TextView) menuCatView.findViewById(R.id.cat_text);
				catText.setText(currObj.getCatName());
				menuCatView.setTag(R.id.dish_name,-1);
				return menuCatView;
			}
			else
			{
				LinearLayout menuDataView = (LinearLayout) inflater.inflate(R.layout.menu_items, null);
				TextView dishName = (TextView) menuDataView.findViewById(R.id.dish_name);
				TextView dishPrice = (TextView) menuDataView.findViewById(R.id.dish_price);
				TextView dishDesc = (TextView) menuDataView.findViewById(R.id.dish_spec);
				if (objNew.getDISH_NAME() != null)
				{
					dishName.setText(objNew.getDISH_NAME());
				}
				dishPrice.setText(objNew.getDISH_COST());
				if (objNew.getDISH_DESC() != null)
				{
					dishDesc.setText(objNew.getDISH_DESC());
				}
				menuDataView.setTag(R.id.dish_name, objNew.getDISH_ID());
				return menuDataView;
			}
		}
		return null;
	}

}
